<?php

!extension_loaded('curl') && die('The curl extension is not loaded.');

class priceData {

    var $channel;
    var $weight;
    var $price;
    var $currency;
    var $expressTime;

}

class SingleData {

    var $outstoreno;
    var $changeno;
    var $turnno;
    var $channel;
    var $destination;
    var $num;
    var $weight;
    var $bagweight;
    var $name;
    var $zipcode;
    var $recipients;
    var $crdatetime;
}

class costpriceData {

    var $datetime;
    var $billid;
    var $num;
    var $weight;
    var $bagweight;
    var $channel;
    var $destination;
    var $name;
    var $money;
    var $currency;
    var $remark;

}

class searchFilter {

    // 目的地名称
    var $countryName;
    // 目的地缩写
    var $country;
    //实重
    var $rweight;
    //类型
    var $goodstype;
    //WPX
    //长*宽*高
    var $len;
    var $width;
    var $height;
    //货物类型
    var $code;

    function getSearchFileds() {
        return "countryname=" . $this->countryName . "&country=" . $this->country . "&rweight=" . $this->rweight .
                "&goodstype=" . $this->goodstype . "&len=" . $this->len . "&width=" . $this->width . "&height=" .
                $this->height . "&code=" . $this->code;
    }

}

class singleFilter {

    var $datetime;
    var $billid;
    function getSingleFileds() {
        if($this->datetime < date("Y-m-d")){
            $begindate = $this->datetime;
            $enddate = date('Y-m-d',strtotime("$this->datetime +1 day"));
        } else {
            $begindate = "2013-04-01";
            $enddate = date("Y-m-d");
        }
        return "inbegindate=".$begindate."&inenddate=".$enddate;
    }

}
class costpriceFilter {

    var $datetime;

    function getCostpriceFileds() {
        if($this->datetime < date("Y-m-d")){
            $where = "&beginhour=23&endhour=0";
            $begindate = $this->datetime;
            $enddate = date("Y-m-d");
        } else {
            $where = "&beginhour=0&endhour=0";
            $begindate = "2013-04-01";
            $enddate = date("Y-m-d");
        }
        return "begindate=" . $begindate . "&enddate=" . $enddate . $where;
    }

}

class Uuch_KingTransAPI {

    var $host;
    var $loginUrl = "/client/Logon?action=logon";
    var $postPriceUrl = "/client/Client?action=price";
    var $postsingleUrl = "/client/Client?action=trackList";
    var $postCostPriceUrl = "/client/Client?action=clientCheck";
    var $userid;
    var $password;
    var $loginFileds;
    var $cookie_file;

    function Uuch_KingTransAPI($host, $userName, $pwd) {
        $this->host = $host;
        $this->userid = $userName;
        $this->password = $pwd;
        $this->loginFileds = "userid=" . $this->userid . "&password=" . $this->password;
        $this->cookie_file = dirname(__FILE__) . '/cookie.txt';
    }

    private function login() {
        $ch = curl_init($this->host . $this->loginUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->loginFileds);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
        curl_exec($ch);
        curl_close($ch);
    }

    private function getArray($table) {
        $table = preg_replace("'<table[^>]*?>'si", "", $table);
        $table = preg_replace("'<tr[^>]*?>'si", "", $table);
        $table = preg_replace("'<td[^>]*?>'si", "", $table);
        $table = str_replace("</tr>", "{tr}", $table);
        $table = str_replace("</td>", "{td}", $table);
        //去掉 HTML 标记
        $table = preg_replace("'<[\/\!]*?[^<>]*?>'si", "", $table);
        //去掉空白字符 
        $table = preg_replace("'([\r\n])[\s]+'", "", $table);
        $table = str_replace(" ", "", $table);
        $table = str_replace(" ", "", $table);
        $table = explode('{tr}', $table);
        array_pop($table);
        $td_array = array();
        foreach ($table as $key => $tr) {
            $td = explode('{td}', $tr);
            array_pop($td);
            $td_array[] = $td;
        }
        return $td_array;
    }

    private function getPriceHtml(searchFilter $searchFilter) {
        $priceUrl = $this->host . $this->postPriceUrl;
        $ch = curl_init($priceUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        $postFields = $searchFilter->getSearchFileds();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
        $html = curl_exec($ch);
        curl_close($ch);
        return $html;
    }

    function getPrice(searchFilter $searchFilter) {
        $this->login();
        $html = $this->getPriceHtml($searchFilter);
        $seletpreg = '%<table.*?>(.*?)</table>%si';
        preg_match_all($seletpreg, $html, $match);
        $html = $this->getArray($match[0][1]);
        $result = array();
        $i = 0;
        foreach ($html as $value) {
            $i++;
            if (count($value) > 1 && $i > 1) {
                array_pop($value);
                array_pop($value);
                $model = new priceData();
                $model->channel = iconv("GB2312//IGNORE", "UTF-8",  $value[0]);
                $model->weight = iconv("GB2312//IGNORE", "UTF-8",  $value[1]);
                $model->price = iconv("GB2312//IGNORE", "UTF-8",  $value[2]);
                $model->currency =iconv("GB2312//IGNORE", "UTF-8",   $value[3]);
                $model->expressTime =iconv("GB2312//IGNORE", "UTF-8",   $value[4]);
                array_push($result, $model);
            }
        }
        return $result;
    }

    private function getSingleHtml(singleFilter $singleFilter) {
        $priceUrl = $this->host . $this->postsingleUrl;
        $ch = curl_init($priceUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        $postFields = $singleFilter->getSingleFileds();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
        $html = curl_exec($ch);
        curl_close($ch);
        return $html;
    }

    function getSingleSd(singleFilter $singleFilter) {
        $this->login();
        $html = $this->getSingleHtml($singleFilter);
        $seletpreg = '%<table.*?>(.*?)</table>%si';
        $match = array();
        preg_match_all($seletpreg, $html, $match);
        $html1 = "";
        if($match != array() && isset($match[0][1])){
            $html1 = $this->getArray($match[0][1]);
        }
        $result = array();
        $i = 0;
        if($html1){
            foreach ($html1 as $value) {
                $i++;
                if (count($value) > 1 && $i > 1) {
                    array_pop($value);
                    array_pop($value);
                    $model = new SingleData();
                    $model->outstoreno = $value[1];
                    $model->changeno = $value[2];
                    $model->turnno = $value[3];
                    $model->channel = $value[4];
                    $model->destination = $value[5];
                    $model->num = $value[6];
                    $model->weight = $value[7];
                    $model->bagweight = $value[8];
                    $model->name = $value[9];
                    $model->zipcode = $value[10];
                    $model->recipients = $value[11];
                    $model->crdatetime = date("Y") . "-" . $value[12];
                    array_push($result, $model);
                }
            }
        }
        return $result;
    }

    function getSingleHly(singleFilter $singleFilter) {
        $this->login();
        $html = $this->getSingleHtml($singleFilter);
        $seletpreg = '%<table.*?>(.*?)</table>%si';
        $match = array();
        preg_match_all($seletpreg, $html, $match);
        $html1 = "";
        if($match != array() && isset($match[0][1])){
            $html1 = $this->getArray($match[0][1]);
        }
        $result = array();
        $i = 0;
        if($html1){
            foreach ($html1 as $value) {
                $i++;
                if (count($value) > 1 && $i > 1) {
                    array_pop($value);
                    array_pop($value);
                    $model = new SingleData();
                    $model->outstoreno = $value[1];
                    $model->changeno = $value[3];
                    $model->turnno = $value[4];
                    $model->channel = $value[5];
                    $model->destination = $value[6];
                    $model->num = $value[7];
                    $model->weight = $value[8];
                    $model->bagweight = $value[9];
                    $model->name = $value[10];
                    $model->zipcode = $value[11];
                    $model->recipients = $value[12];
                    $model->crdatetime = date("Y") . "-" . $value[13];
                    array_push($result, $model);
                }
            }
        }
        return $result;
    }

    private function getCostPriceHtml(costpriceFilter $costpriceFilter) {
        $priceUrl = $this->host . $this->postCostPriceUrl;
        $ch = curl_init($priceUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        $postFields = $costpriceFilter->getCostpriceFileds();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
        $html = curl_exec($ch);
        curl_close($ch);
        return $html;
    }

    function getCostPriceHly(costpriceFilter $costpriceFilter) {
        $this->login();
        $html = $this->getCostPriceHtml($costpriceFilter);
        $seletpreg = '%<table.*?>(.*?)</table>%si';
        $match = array();
        preg_match_all($seletpreg, $html, $match);
        $html1 = "";
        if($match != array() && isset($match[0][2])){
            $html1 = $this->getArray($match[0][2]);
        }
        $result = array();
        $i = 0;
        if($html1){
            foreach ($html1 as $value) {
                $i++;
                if (count($value) > 1 && $i > 1) {
                    if ($value[1] != "") {
                        $model = new costpriceData();
                        $model->datetime = $value[0];
                        $model->billid = $value[1];
                        $model->num = $value[2];
                        $model->weight = $value[3];
                        $model->bagweight = $value[4];
                        $model->channel = $value[5];
                        $model->destination = $value[6];
                        $model->name = $value[7];
                        $model->money = $value[8];
                        $model->currency = $value[9];
                        $model->remark = $value[10];
                        array_push($result, $model);
                    }
                }
            }
        }
        return $result;
    }
    function getCostPriceSd(costpriceFilter $costpriceFilter) {
        $this->login();
        $html = $this->getCostPriceHtml($costpriceFilter);
        $seletpreg = '%<table.*?>(.*?)</table>%si';
        $match = array();
        preg_match_all($seletpreg, $html, $match);
        $html1 = "";
        if($match != array() && isset($match[0][2])){
            $html1 = $this->getArray($match[0][2]);
        }
        $result = array();
        $i = 0;
        if($html1){
            foreach ($html1 as $value) {
                $i++;
                if (count($value) > 1 && $i > 1) {
                    if ($value[1] != "") {
                        $model = new costpriceData();
                        $model->datetime = $value[0];
                        $model->billid = $value[1];
                        $model->num = $value[2];
                        $model->weight = $value[3];
                        $model->bagweight = $value[4];
                        $model->channel = $value[5];
                        $model->destination = $value[6];
                        $model->name = $value[7];
                        $model->money = $value[8];
                        $model->currency = $value[9];
                        $model->remark = $value[11];
                        array_push($result, $model);
                    }
                }
            }
        }
        return $result;
    }

}

?>
